// Sage Android Client.
// Connect to a Sage server, remotely execute commands and display results.
//
// Copyright (C) 2010, Harald Schilly <harald.schilly@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

/**
 * 
 */

package org.sagemath.android;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * @author Harald Schilly
 */
class Server {
  String server, username, password;
  
  /**
   * it's a convention that there has to be a field called CREATOR.
   */
  final public static Parcelable.Creator<Server> CREATOR = new Parcelable.Creator<Server>() {
    
    @Override
    final public Server createFromParcel(Parcel in) {
      return new Server(in);
    }
    
    @Override
    final public Server[] newArray(int size) {
      return new Server[size];
    }
  };
  
  public Server(Parcel in) {
    readFromParcel(in);
  }
  
  public Server(String server, String username, String password) {
    this.server = server;
    this.username = username;
    this.password = password;
  }
  
  /**
   * 
   */
  public Server() {
    // TODO Auto-generated constructor stub
  }
  
  /**
   * AIDL serialization, must match with writeToParcel.
   * 
   * @param in
   */
  final void readFromParcel(Parcel in) {
    server = in.readString();
    username = in.readString();
    password = in.readString();
  }
  
  /**
   * AIDL serialization, must match with readFromParcel
   * 
   * @param reply
   * @param parcelableWriteReturnValue
   */
  final void writeToParcel(Parcel out, int parcelableWriteReturnValue) {
    out.writeString(server);
    out.writeString(username);
    out.writeString(password);
  }
  
  /**
   * Determine if two probably distinct objects are equal.
   * 
   * @return true, if they have the same field values.
   */
  @Override
  public boolean equals(Object other) {
    if (this == other) {
      return true;
    }
    if (other instanceof Server) {
      Server so = (Server) other;
      if (this.server == so.server && this.username == so.username && this.password == so.password) {
        return true;
      }
    }
    return false;
  }
  
  /**
   * This class is used in a HashSet and has no final fields. We also have to
   * override {@link equals}.
   */
  @Override
  public int hashCode() {
    return 0; // no immutable fields!
  }
  
  @Override
  public String toString() {
    return server;
  }
}
